﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using xf.Entitys;
using xf.Service;

namespace xf
{
    public partial class FGoodsList : Form
    {
        private static FGoodsList fGoodsList = null;

        private static FGoodsList self = null;

        public FGoodsList()
        {
            InitializeComponent();
            self = this;
        }

        public static void StaticReloadCustomerList()
        {
            self.GetList();
        }

        /// <summary>
        /// 保存/修改产品信息界面，只能同时显示一个
        /// </summary>
        /// <returns></returns>
        public static FGoodsList CreateInstance()
        {
            if ((fGoodsList == null) || fGoodsList.IsDisposed)
            {
                fGoodsList = new FGoodsList();
            }
            return fGoodsList;
        }

        /// <summary>
        /// 显示“添加商品信息”界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addGoodsBtn_Click(object sender, EventArgs e)
        {
            FSaveGoods fSaveGoods = new FSaveGoods();
            fSaveGoods.Show();
        }

        /// <summary>
        /// 加载“产片列表”
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadCustomerList(object sender, EventArgs e)
        {
            GetList();
        }

        /// <summary>
        /// 获取查询产品列表
        /// </summary>
        public void GetList()
        {
            Dictionary<string, string> queryParam = new Dictionary<string, string> {
                { "Goods_id", goodsIdTextBox.Text.Trim()},
                { "Goods_name", goodsNameTextBox.Text.Trim()},
            };
            goodsDataGridView.DataSource = GoodsService.GetInstance().All(queryParam);
        }

        /// <summary>
        /// 删除产品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void delGoodsBtn_Click(object sender, EventArgs e)
        {
            GOODS_NAME goodsName = goodsDataGridView.CurrentRow.DataBoundItem as GOODS_NAME;
            string goodsId = goodsName.Goods_id.ToString();
            DialogResult dialogResult = MessageBox.Show("您确定要退出删除该产品【产品ID：" + goodsId + "】吗？", "退出提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                // 删除操作
                if (GoodsService.GetInstance().Delete(goodsName))
                {
                    MessageBox.Show("删除产品成功，产品ID：" + goodsId);
                    // 后期优化，这里需要重新手动刷新一遍
                    GetList();
                }
                else
                {
                    MessageBox.Show("删除产品失败，产品ID：" + goodsId);
                }
            }
        }

        private void searchGoodsBtn_Click(object sender, EventArgs e)
        {
            GetList();
        }

        private void exitBtn_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }
    }
}
